<template>
  <div class="page-container">
    <div class="page-content">
      <div class="panel-container">
        <div class="panel-content">
          <div class="panel-line space-between">
            <div class="panel-left">
              <el-radio-group v-model="tabActive" style="margin-right: 30px" size="mini" @change="handleTabChange">
                <el-radio-button v-for="item in tabList" :key="item.value" :label="item.value">{{ item.label }}({{ item.num }})</el-radio-button>
              </el-radio-group>
              <el-button class="show-search" type="text" @click="handleShowSearch">
                <span v-if="showSearch"> 收起搜索 </span>
                <span v-else> 展开搜索 </span>
                <i class="el-icon-arrow-down" :class="{ rotate: showSearch }"></i>
              </el-button>
            </div>
          </div>
          <!--搜索-->
          <SearchUiNew style="margin-top: 10px" v-show="showSearch" :searchFormArr="searchFormArr" @searchParma="searchParma"></SearchUiNew>
        </div>
      </div>
      <!--数据信息-->
      <div class="panel-container no-margin">
        <el-table stripe :data="dataList" size="mini" :header-cell-style="{ background: '#F5F5F5', color: '#000000' }" ref="table" style="width: 100%" v-loading="dataListLoading">
          <af-table-column align="center" header-align="center" show-overflow-tooltip label="ID" prop="number_id"></af-table-column>
          <af-table-column align="center" header-align="center" show-overflow-tooltip label="信息标题" prop="title" width="200"></af-table-column>
          <af-table-column align="center" header-align="center" show-overflow-tooltip label="公司名称" prop="company_name"></af-table-column>
          <af-table-column align="center" header-align="center" show-overflow-tooltip label="锁定开始时间" prop="lock_start_time"></af-table-column>
          <af-table-column align="center" header-align="center" show-overflow-tooltip label="锁定结束时间" prop="lock_end_time"></af-table-column>
          <af-table-column align="center" header-align="center" show-overflow-tooltip label="实际结束时间" prop="open_lock_time" width="150"></af-table-column>
          <af-table-column align="center" header-align="center" show-overflow-tooltip label="状态" prop="is_lock" width="150">
            <template slot-scope="scope">
              {{ getLockSts(scope.row.is_lock) }}
            </template>
          </af-table-column>
        </el-table>
        <el-pagination
          small
          background
          :current-page="pageNumber"
          :page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pageTotal"
          @current-change="currentChangeHandle"
          @size-change="sizeChangeHandle"
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 20px; text-align: right"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { apiAssetLockList } from '@/api/assetsPublic'

export default {
  name: 'LockRecord',
  components: {},
  data() {
    return {
      showSearch: false,
      tabActive: '',
      tabList: [
        { value: '', label: '全部', num_name: 'total', num: 0 },
        { value: 2, label: '已锁定', num_name: 'locked', num: 0 },
        { value: 3, label: '已解锁', num_name: 'unlock', num: 0 },
      ],
      dateRange: '',
      searchFormArr: [
        {
          fieldName: '检索:',
          type: 'input',
          field: 'search',
          placeholder: '请输入检索',
        },
        {
          fieldName: '',
          type: 'select',
          field: 'search_type',
          labelWidth: '0px',
          default: 'title',
          clearable: false,
          option: [
            { value: 'title', label: '按标题' },
            { value: 'desc', label: '按内容' },
            { value: 'user_name', label: '按联系人' },
            { value: 'user_phone', label: '按联系电话' },
            { value: 'number_id', label: '按id' },
            { value: 'company_name', label: '按公司名称' },
          ],
          placeholder: '请选择',
        },
        {
          fieldName: '锁定开始时间:',
          type: 'timePicker',
          field: 'time',
          placeholder: '请选择锁定开始时间',
          timeType: 'daterange',
          format: 'yyyy-MM-dd',
        },
      ],

      searchParams: {
        title: '',
        desc: '',
        user_name: '',
        user_phone: '',
        number_id: '',
        company_name: '',
        start_time: '',
        end_time: '',
      },
      dataList: [],
      pageNumber: 1,
      pageSize: 10,
      pageTotal: 0,
      dataListLoading: false,
    }
  },
  activated() {
    this.$refs.table.doLayout()
  },
  mounted() {
    this.getDataList()
  },
  destroyed() {},
  methods: {
    handleShowSearch() {
      this.showSearch = !this.showSearch
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      let info = { pageNumber: this.pageNumber, pageSize: this.pageSize, is_lock: this.tabActive }
      info = Object.assign(info, this.searchParams)
      apiAssetLockList(info)
        .then((res) => {
          const { data, total, others } = res
          this.dataList = data
          this.pageTotal = total
          this.dataListLoading = false
          this.tabList.forEach((item) => {
            item.num = others[item.num_name]
          })
          this.$refs.table.doLayout()
        })
        .catch((e) => {
          this.dataList = []
          this.pageTotal = 0
          this.dataListLoading = false
        })
    },
    // 列表查询
    searchParma(e) {
      let list = ['search', 'search_type', 'time']
      this.searchFormArr.forEach((item) => {
        if (list.includes(item.field)) return

        if (e[item.field]) {
          this.searchParams[item.field] = e[item.field]
        } else {
          this.searchParams[item.field] = ''
        }
      })
      const formItem1 = this.searchFormArr.find((item) => item.field === 'search_type')
      const option1 = formItem1.option
      option1.forEach((item) => {
        this.searchParams[item.value] = ''
      })
      if (e.search) {
        this.searchParams[e.search_type] = e.search
      }
      if (e.time) {
        this.searchParams.start_time = e.time[0]
        this.searchParams.end_time = e.time[1]
      } else {
        this.searchParams.start_time = this.searchParams.end_time = ''
      }
      this.pageNumber = 1
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageNumber = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageNumber = val
      this.getDataList()
    },
    // 获取锁定状态
    getLockSts(lock) {
      const item = this.tabList.find((item) => item.value === lock)
      if (item) {
        return item.label
      } else {
        return ''
      }
    },
    // 切换tab
    handleTabChange() {
      this.pageNumber = 1
      this.getDataList()
    },
  },
}
</script>
